﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using HZ.MBSM.DBUtility;

public partial class Agency_IsBidGoods : System.Web.UI.Page
{
    public string _strProjectId = "";
    protected void Page_Load(object sender, EventArgs e)
    {

        System.Collections.Generic.List<HZ.MBSM.DAL.AccountType> _AccountType = new System.Collections.Generic.List<HZ.MBSM.DAL.AccountType>();
        _AccountType.Add(HZ.MBSM.DAL.AccountType.enumAttorney);
        if (HZ.MBSM.BLL.Login.CheckAssert(_AccountType))
        {
            _strProjectId = SysFun.IsSQL(SysFun.ToTrim(Request.QueryString["projectid"]));

            if (Request.Form["SubSave"] == "保  存")
            {
                string Bidid = "";
                String BeiZhu = "", strSql;
                Bidid = SysFun.IsSQL(SysFun.ToTrim(Request.Form["Bidid"]));
                Db db = new Db();
                if (Bidid != "")
                {
                    string[] KBBID = Bidid.Length == 0 ? new string[] { "" } : Bidid.Substring(0).Split(new char[] { ',' });
                    int nNumber = 0;
                    strSql = "";
                    string _strBidPrice = "", _strIsBid = "";
                    for (int i = 0; i < KBBID.Length; i++)
                    {
                        nNumber++;
                        BeiZhu = Request.Form["beizhu" + KBBID[i]];
                        _strBidPrice = Request.Form["txtBidPrice" + KBBID[i]];
                        _strIsBid = Request.Form["chkIsbid" + KBBID[i]];
                        if (!SysFun.IsNumeric(_strBidPrice))
                        {
                            Response.Write("<script>window.alert('限价报价只能为数字！');</script>");
                        }
                        else
                        {
                            _strBidPrice = Convert.ToDecimal(_strBidPrice).ToString("N2");
                            if (SysFun.ToDec(_strBidPrice) < 0 || SysFun.ToDec(_strBidPrice) > 99999999)
                            {
                                Response.Write("<script>window.alert('限价报价数据有误！');</script>");
                            }
                            else
                            {
                                if (_strIsBid == null)
                                {
                                    strSql += "@" + "Update project_bid Set CheckStatus =1, memo10='" + BeiZhu + "',bidprice=0,isbid=0  Where Bidid=" + KBBID[i].ToString();
                                }
                                else
                                {
                                    strSql += "@" + "Update project_bid Set CheckStatus =1, memo10='" + BeiZhu + "',bidprice=" + _strBidPrice + ",isbid=1  Where Bidid=" + KBBID[i].ToString();
                                }
                            }
                        }


                    }
                    if ((strSql.Length > 0) && (nNumber == KBBID.Length))
                    {
                        db.ExecuteTransaction(strSql.Substring(1).Split(new char[] { '@' }), 30);
                        HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)Session["Account"];
                        HZ.MBSM.BLL.LogManage.RecordLoginDetail(_Account.AccountName, Request.UserHostAddress, "更新", "project_bid", strSql, "../Agency/IsBidGoods.aspx");

                        Response.Write("<script>alert('数据保存成功！');</script>");
                    }
                }
                db.Close();

            }
            showGridView();
        }
    }
    public void showGridView()
    {
        int _intPrint =0;
        if (Request.Form["PrintExcel"] == "打  印")
        {
            _intPrint = 1;
        }
        int i = 0;
        string strConditions = fctConditions();
        Db db = new Db();
        DbPage dbPage = new DbPage(db);
        dbPage.Table = "Bid_goods_product_cs_tb_agent";  //要查询的表名
        dbPage.Key = "bidid";                                               //表的主键    
        dbPage.Where = strConditions;                                           //查询条件    
        dbPage.SelectField = "bidid,goodsid,ProductCode,ProductName,Outlookc,Medicinemodel,Factor,Unit+'/'+PACKUNIT AS PACK, CASE QualityLevels " + ConfigurationManager.AppSettings["QualityLevels"].ToString() + " END AS QualityLevels,UserCode_SC,CompanyName_SC,UserCode_TB,CompanyName_TB,SPPrice,LimitPrice*factor as limitprice,IsBid,SecondPrice ,ThirdPrice, MEMO10,bidprice,case status when '0' then '限价' when '1' then '竟价' when '2' then '不竞不议' when '3' then '备案采购' end as status ";//要显示的字段
        dbPage.PageIndex = SysFun.ToInt(Request["CurrentlyPageIndex"]);          //要显示第几页   
        if (_intPrint == 0)
        {
            dbPage.PageSize = 10;                                                    //没有显示记录条数   
        }
        else
        {
            dbPage.PageSize = 0;                                                    //显示记录条数   ,0代表全部
        }
        dbPage.OrderBy = "ProductCode,QualityLevels,bidid";//排序字段


        System.Text.StringBuilder strHtml = new System.Text.StringBuilder();
        strHtml.Append("<table width='98%' border='0' align='center' cellpadding='2' cellspacing='1' class='table_table'>");
        strHtml.Append("<tr>");
        strHtml.Append("<td class='table_title' >药品流水号</td>");
        strHtml.Append("<td class='table_title' >品目号</td>");
        strHtml.Append("<td class='table_title' >通用名</td>");
        strHtml.Append("<td class='table_title' >剂型</td>");
        strHtml.Append("<td class='table_title' >规格</td>");
        strHtml.Append("<td class='table_title' >转换系数</td>");
        strHtml.Append("<td class='table_title' >包装</td>");
        strHtml.Append("<td class='table_title' >质量层次</td>");
        strHtml.Append("<td class='table_title' >生产企业名称</td>");
        strHtml.Append("<td class='table_title' >注册号</td>");
        strHtml.Append("<td class='table_title' >投标人</td>");
        strHtml.Append("<td class='table_title' >注册号</td>");
        strHtml.Append("<td class='table_title' >状态</td>");
        strHtml.Append("<td class='table_title' >限价</td>");
        strHtml.Append("<td class='table_title' >投标报价</td>");
        strHtml.Append("<td class='table_title' >限价报价</td>");
        strHtml.Append("<td class='table_title' >竞价报价</td>");
        strHtml.Append("<td class='table_title' >中标价</td>");
        strHtml.Append("<td class='table_title' >说明</td>");
        strHtml.Append("<td class='table_title' >是否中标</td>");
        strHtml.Append("</tr>");
        System.Data.IDataReader dr = dbPage.ExecuteReader();
        string _strIsBid = "0";
        while (dr.Read())
        {
            if (i == 0)
            {
                strHtml.Append("<tr class='table_td'>");
                i++;
            }
            else
            {
                strHtml.Append("<tr class='table_td2'>");
                i--;
            }
            strHtml.Append("<td><Input Name='BidId' type='Hidden' Value='" + SysFun.ToTrim(dr["Bidid"].ToString()) + "'>" + dr["goodsid"].ToString() + "</td>");
            strHtml.Append("<td>" + dr["ProductCode"].ToString() + "</td>");
            strHtml.Append("<td>" + dr["ProductName"].ToString() + "</td>");
            strHtml.Append("<td>" + dr["Medicinemodel"].ToString() + "</td>");
            strHtml.Append("<td>" + dr["Outlookc"].ToString() + "</td>");
            strHtml.Append("<td>" + dr["Factor"].ToString() + "</td>");
            strHtml.Append("<td>" + dr["pack"].ToString() + "</td>");
            strHtml.Append("<td>" + dr["QualityLevels"].ToString() + "</td>");
            strHtml.Append("<td>" + dr["CompanyName_SC"].ToString() + "</td>");
            strHtml.Append("<td>" + dr["UserCode_SC"].ToString() + "</td>");
            strHtml.Append("<td>" + dr["CompanyName_TB"].ToString() + "</td>");
            strHtml.Append("<td>" + dr["UserCode_TB"].ToString() + "</td>");
            strHtml.Append("<td>" + dr["status"].ToString() + "</td>");
            strHtml.Append("<td>" + SysFun.ToDec(dr["LimitPrice"].ToString()).ToString("0.00") + "</td>");
            strHtml.Append("<td>" + SysFun.ToDec(dr["SPPrice"].ToString()).ToString("0.00") + "</td>");
            strHtml.Append("<td>" + SysFun.ToDec(dr["SecondPrice"].ToString()).ToString("0.00") + "</td>");
            strHtml.Append("<td>" + SysFun.ToDec(dr["ThirdPrice"].ToString()).ToString("0.00") + "</td>");
            if (_intPrint == 0)
            {
                strHtml.Append("<td><input type='text' name='txtBidPrice" + dr["Bidid"].ToString() + "' value='" + SysFun.ToDec(dr["bidprice"].ToString()).ToString("0.00") + "' style='width:65' /></td>");
                strHtml.Append("<td><textarea  name='beizhu" + dr["Bidid"].ToString() + "' col='10' rows='3' />" + SysFun.ToTrim(dr["memo10"].ToString()) + "</textarea></td>");
                _strIsBid = dr["isbid"].ToString();
                if (_strIsBid == "0")
                {
                    strHtml.Append("<td><input type='checkbox' name='chkIsbid" + dr["Bidid"].ToString() + "'  /></td>");
                }
                else
                {
                    strHtml.Append("<td><input type='checkbox' name='chkIsbid" + dr["Bidid"].ToString() + "'  checked=checked /></td>");
                }
            }
            else
            {
                strHtml.Append("<td>" + SysFun.ToDec(dr["bidprice"].ToString()).ToString("0.00") + "</td>");
                strHtml.Append("<td>" + SysFun.ToTrim(dr["memo10"].ToString()) + "</td>");
                _strIsBid = dr["isbid"].ToString();
                if (_strIsBid == "0")
                {
                    strHtml.Append("<td>否</td>");
                }
                else
                {
                    strHtml.Append("<td>是</td>");
                }
            }

            strHtml.Append("</tr>");
        }
        strHtml.Append("</table>");
        if (_intPrint != 0)
        {
            PrintExcel(strHtml);
        }
        else
        {
            Label1.Text = strHtml.ToString();
        }
        
        //GridView1.DataSource = dr;
        //GridView1.DataBind();
        dr.Close();
        dr.Dispose();
        dbPage.Db.Close();
        db.Close();

        this.DbPageFlip1.DbPage = dbPage;//将数据传给控键



    }
    protected void PrintExcel(System.Text.StringBuilder strHtml)
    {

        Response.Clear();
        Response.Buffer = true;
        Response.Charset = "GB2312";
        // 添加头信息，为"文件下载/另存为"对话框指定默认文件名 
        // Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(accountid.ToString()) + ".xls");
        Response.AddHeader("Content-Disposition", "attachment; filename=11.html");
        Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
        Response.ContentEncoding = System.Text.Encoding.UTF7;  //解决将gridview导出成excel时出现乱码  
        Response.ContentType = "application/ms-excel";
        this.EnableViewState = false;
        System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
        oStringWriter.Write(strHtml.ToString());
        //this.Label1.RenderControl(oHtmlTextWriter);
        // 把文件流发送到客户端 
        Response.Write(strHtml.ToString());
        // 停止页面的执行
        Response.End();

    }
    public override void VerifyRenderingInServerForm(Control control)
    {
        //OverRide　为了使导出成Excel可行！
    }
    /// <summary>
    /// 生成查询条件
    /// </summary>
    /// <returns></returns>
    public string fctConditions()
    {
        string strtemp, _GoodsId;

        string _strIsBid = lstIsBid.SelectedValue;

        if (_strIsBid == "1")
        {
            strtemp = " projectid=" + _strProjectId + " and CheckStatus=1 and smprice >0  and  isbid=1  and";
        }
        else
        { 
          strtemp = " projectid=" + _strProjectId + " and CheckStatus=1 and smprice >0  and accountid_tb in (select accountid from ProjectCompanyAttribution where projectid=" + _strProjectId + " and BidSuccess=1)  and";
        }
      
        //流水号
        _GoodsId = SysFun.ToTrim(txtGoodsId.Text);
        if (_GoodsId != "")
        {
            strtemp += " GoodsId like '%" + _GoodsId + "%' and";
        }

        //生产企业名称
        string _strCompanyName_SC = SysFun.ToTrim(txtSC.Text);
        if (_strCompanyName_SC != "")
        {
            strtemp += " CompanyName_SC like '%" + _strCompanyName_SC + "%' and";
        }

        //品目号
        string _strProductCode = ProductCode.Text.ToString().Trim();
        if (_strProductCode != "")
        {
            strtemp += " ProductCode like '%" + _strProductCode + "%' and";
        }

        //药品名称
        string _strProductName = ProductName.Text.ToString().Trim();
        if (_strProductName != "")
        {
            strtemp += " (ProductName like '%" + _strProductName + "%'or SpellingCode like '%" + _strProductName + "%') and";
        }

        //规格
        string _strOutlookc = Outlookc.Text.ToString().Trim();
        if (_strOutlookc != "")
        {
            strtemp += " Outlookc like '%" + _strOutlookc + "%' and";
        }

        //投标人名称
        string _strCompanyName_TB = SysFun.ToTrim(txtCompanyName_TB.Text);
        if (_strCompanyName_TB != "")
        {
            strtemp += " CompanyName_TB like '%" + _strCompanyName_TB + "%' and";
        }

        //投标人注册号
        string _strUserCode_TB = txtCompanyUserCode.Text.ToString().Trim();
        if (_strUserCode_TB != "")
        {
            strtemp += " UserCode_TB='" + _strUserCode_TB + "' and";
        }

        string _strStatus = DDStatus.SelectedValue;
        if (_strIsBid == "0")
        {
            strtemp += " isbid=" + _strIsBid + " and"; 
        }
        if (_strStatus != "4")
        {
            strtemp += " status=" + _strStatus + " and";
        }
        if (strtemp != "")
        {
            strtemp = "  " + strtemp.Substring(0, strtemp.Length - 3);
        }

        return strtemp;

    }



    protected void Button5_Click(object sender, EventArgs e)
    {

    }
}
